package com.scoy.honeymei.activity.home;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.base.BaseActivity;
import com.scoy.honeymei.bean.TripDetailBean;
import com.scoy.honeymei.dialog.ShareDialog;
import com.scoy.honeymei.http.HpCallback;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.MeConstant;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.SPHelper;
import com.scoy.honeymei.utils.TimeUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: TripDetailActivity$
 * @Description: 出行详情
 * @Author: GaoZhan
 * @CreateDate: 2019/9/6/0006$ 上午 9:29$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/9/6/0006$ 上午 9:29$
 * @UpdateRemark: 更新内容
 */
public class TripDetailActivity extends BaseActivity {

    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.sign_tv)
    TextView signTv;
    @BindView(R.id.title_llt)
    LinearLayout titleLlt;
    @BindView(R.id.trd_gotime_tv)
    TextView trdGotimeTv;
    @BindView(R.id.trd_arrivetime_tv)
    TextView trdArrivetimeTv;
    @BindView(R.id.trd_startadd_tv)
    TextView trdStartaddTv;
    @BindView(R.id.trd_jt_iv)
    ImageView trdJtIv;
    @BindView(R.id.trd_endadd_tv)
    TextView trdEndaddTv;
    @BindView(R.id.trd_needmoney_tv)
    TextView trdNeedmoneyTv;
    @BindView(R.id.trd_tv0)
    TextView trdTv0;
    @BindView(R.id.trd_tv1)
    TextView trdTv1;
    @BindView(R.id.trd_phone_tv)
    TextView trdPhoneTv;
    @BindView(R.id.trd_tv2)
    TextView trdTv2;
    @BindView(R.id.trd_name_tv)
    TextView trdNameTv;
    @BindView(R.id.trd_tv3)
    TextView trdTv3;
    @BindView(R.id.trd_signccard_tv)
    TextView trdSignccardTv;
    @BindView(R.id.trd_tv4)
    TextView trdTv4;
    @BindView(R.id.trd_allnum_tv)
    TextView trdAllnumTv;
    @BindView(R.id.trd_tv5)
    TextView trdTv5;
    @BindView(R.id.trd_leftnum_tv)
    TextView trdLeftnumTv;
    @BindView(R.id.ct_subtract_tv)
    TextView ctSubtractTv;
    @BindView(R.id.ct_num_tv)
    TextView ctNumTv;
    @BindView(R.id.ct_add_tv)
    TextView ctAddTv;
    @BindView(R.id.trd_price_tv)
    TextView trdPriceTv;
    @BindView(R.id.trd_buy_tv)
    TextView trdBuyTv;

    @BindView(R.id.trd_endtime_tv)
    TextView trdEndtimeTv;
    @BindView(R.id.trd_bookbtm_llt)
    LinearLayout trdBookbtmLlt;

    private TripDetailActivity mContext;
    private int tId;
    private TripDetailBean tripDetailBean;
    private int leftSeat = 0;
    private String fxUrl;
    private String fxName;
    private String fxContent;
    private String fxImage;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trip_detail);
        ButterKnife.bind(this);
        mContext = TripDetailActivity.this;
        tId = getIntent().getIntExtra("tId", 0);

        initNormal();

        httpTripDetail();

    }

    @Override
    public void initNormal() {
        //  titleTv.setText(R.string.shengshi);
        MyUtil.setStatusBar(mContext, getWindow(), true, R.color.colorMainBlue);
        titleLlt.setPadding(0, MyUtil.getStatusBarHeight(mContext), 0, 0);
        signTv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_share_white, 0, 0, 0);
        signTv.setText("分享");
        signTv.setTextColor(getResources().getColor(R.color.colorWhite));

        Uri uri = getIntent().getData();
        if (uri != null) {
            String tttid = uri.getQueryParameter("id");
            try {
                tId = Integer.parseInt(tttid);
            } catch (Exception ignored) {
            }
        }
    }

    @OnClick({R.id.back_iv, R.id.trd_buy_tv, R.id.ct_subtract_tv, R.id.ct_add_tv, R.id.sign_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.trd_buy_tv:
                onNotice();
                break;
            case R.id.sign_tv:
                ShareDialog.newInstance(fxImage, fxName, fxContent, fxUrl).show(getSupportFragmentManager(), "");
                break;
            case R.id.ct_subtract_tv:
                String sPrice = ctNumTv.getText().toString();
                int iSu = Integer.parseInt(sPrice);
                if (iSu > 1) {
                    ctNumTv.setText(iSu - 1 + "");
                } else {
                    MyUtil.mytoast(mContext, "不能再少了");
                }

                trdPriceTv.setText(MyUtil.doubleTo2String(Double.parseDouble(trdNeedmoneyTv.getText().toString()) * Integer.parseInt(ctNumTv.getText().toString())));
                break;
            case R.id.ct_add_tv:
                String sPrice0 = ctNumTv.getText().toString();
                int iSu0 = Integer.parseInt(sPrice0);
                if (iSu0 < leftSeat) {
                    ctNumTv.setText(iSu0 + 1 + "");
                    trdPriceTv.setText(MyUtil.doubleTo2String(Double.parseDouble(trdNeedmoneyTv.getText().toString()) * Integer.parseInt(ctNumTv.getText().toString())));
                }
                break;
        }
    }

    private void httpTripDetail() {
        HttpParams params = new HttpParams();
        params.put("id", tId);
        HpGo.newInstance().HttpGo(this, MyUrl.TRIP_DETAIL, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                tripDetailBean = new Gson().fromJson(o, TripDetailBean.class);
                titleTv.setText(tripDetailBean.getStart_place() + "-" + tripDetailBean.getDestination());
                trdGotimeTv.setText(tripDetailBean.getStarttime() + "出发");
                trdArrivetimeTv.setText("预计约" + tripDetailBean.getDuration());
                trdStartaddTv.setText(tripDetailBean.getStart_place());
                trdEndaddTv.setText(tripDetailBean.getDestination());
                trdNeedmoneyTv.setText(tripDetailBean.getPrice());
                trdPhoneTv.setText(MyUtil.hidePhone(tripDetailBean.getTel()));
                trdNameTv.setText(tripDetailBean.getName());
                trdSignccardTv.setText(tripDetailBean.getBus_no());
                trdAllnumTv.setText(tripDetailBean.getSeat_num() + "座");
                leftSeat = tripDetailBean.getPas_seat_num();

                trdLeftnumTv.setText(tripDetailBean.getPas_seat_num() + "座");
                trdPriceTv.setText(tripDetailBean.getPrice());

                fxUrl = tripDetailBean.getFx_url();
                fxName = tripDetailBean.getFx_title();
                fxContent = tripDetailBean.getFx_miaoshu();
                fxImage = MyUrl.PREFIX_PIC + tripDetailBean.getFx_image();

                long stoptime = tripDetailBean.getStoptime();
                long longTime = TimeUtil.getLongTime();
                if (longTime > stoptime * 1000) {
                    trdBookbtmLlt.setVisibility(View.GONE);
                } else {
                    trdBookbtmLlt.setVisibility(View.VISIBLE);
                }

                trdEndtimeTv.setText(TimeUtil.getLongToDatePHP(stoptime, "yyyy-MM-dd HH:mm"));

            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, code + "," + msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 0x871:
                setResult(0x871);
                finish();
                break;
        }
    }

    /**
     * 提示
     */
    public void onNotice() {
        Dialog dialogNotice = new Dialog(mContext, R.style.BottomDialogStyle);
        View view = View.inflate(mContext, R.layout.dialog_notice, null);
        TextView notTitleTv = view.findViewById(R.id.not_title_tv);
        TextView notSureTv = view.findViewById(R.id.not_sure_tv);
        WebView notWeb = view.findViewById(R.id.not_web);

        notTitleTv.setText("出行温馨提示");
        initWeb(notWeb, MyUrl.PREFIX_PIC + "/detail/html/weAbout.html?type=5");

        dialogNotice.setContentView(view);
        dialogNotice.setCanceledOnTouchOutside(true);
        Window dialogWindow = dialogNotice.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        //    lp.width = (int) (SystemUtil.getScreenWidth(mContext));
        lp.width = (int) getResources().getDimension(R.dimen.dp_300);
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        dialogWindow.setAttributes(lp);
        notSureTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogNotice.dismiss();

                Intent intentb = new Intent(mContext, TripSettleActivity.class);
                intentb.putExtra("num", ctNumTv.getText().toString());
                intentb.putExtra("price", tripDetailBean.getPrice());
                intentb.putExtra("trpId", tripDetailBean.getId());
                intentb.putExtra("leftSeat", leftSeat);
                startActivityForResult(intentb, 0x871);
            }
        });

        dialogNotice.show();
    }


    private void httpDDphone() {
        HttpParams params = new HttpParams();
        HpGo.newInstance().HttpGo(this, MyUrl.TRIP_PHONE, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                //     filmDdphoneTv.setText("调度电话："+o);
            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, code + "," + msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }

}
